package org.client.ui;

import javax.swing.*;

import org.client.view.manage.IMAlterPwClientConServer;
import org.client.view.manage.IMRegisterClientConServer;
import org.pojo.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * @author hzh
 * @time 2012-6-19
 *
 */


public class IMAlterPw extends JFrame implements ActionListener {
	private JPasswordField jPasswordField0, jPasswordField1, jPasswordField2;
	private JTextField jTextField3;
	private JPanel p;
	private JLabel jLabel0,jLabel1,jLabel2,jLabel3,jLabel4;
	private JButton jButton0,jButton1;
	public IMAlterPw() {
		super("修改密码");
		p = new JPanel(null);
		
		ImageIcon backgrd = new ImageIcon("images/aio.png");
		jLabel4 = new JLabel(backgrd);
		jLabel4.setBounds(0, 0, backgrd.getIconWidth(),backgrd.getIconHeight());
		this.getLayeredPane().add(jLabel4,new Integer(Integer.MIN_VALUE));
		JPanel jp = (JPanel) this.getContentPane();
		jp.setOpaque(false);
		p.setOpaque(false);
		
		/*处理组件*/
		jLabel3 = new JLabel("用户名：");
		jTextField3 = new JTextField(20);
		jLabel0 = new JLabel("原密码：");
		jPasswordField0 = new JPasswordField(20);
		jLabel1 = new JLabel("新密码：");
		jPasswordField1 = new JPasswordField(20);
		jLabel2 = new JLabel("确认密码：");
		jPasswordField2 = new JPasswordField(20);
		jButton0 = new JButton("确定");
		jButton1 = new JButton("取消");
		jLabel0.setBounds(10, 60, 70, 30);
		jLabel1.setBounds(10, 100, 70, 30);
		jLabel2.setBounds(10, 140, 70, 30);
		jLabel3.setBounds(10, 20, 70, 30);
		jTextField3.setBounds(85, 20, 180, 30);
		jPasswordField0.setBounds(85, 60, 180, 30);
		jPasswordField1.setBounds(85, 100, 180, 30);	
		jPasswordField2.setBounds(85, 140, 180, 30);
		jButton0.setBounds(30, 205, 80, 30);
		jButton1.setBounds(180, 205, 80, 30);
		p.add(jLabel0);
		p.add(jLabel1);
		p.add(jLabel2);
		p.add(jLabel3);
		p.add(jTextField3);
		p.add(jPasswordField0);
		p.add(jPasswordField1);
		p.add(jPasswordField2);
		p.add(jButton0);
		p.add(jButton1);
		
		//事件监听
		jButton0.addActionListener(this);
		jButton1.addActionListener(this);
		
		//添加组件
		this.add(p);
		this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/im.png"));
		this.setSize(300, 290);
		this.setLocation(500, 400);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		IMAlterPw ap = new IMAlterPw();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if (source == jButton0) {
			btnOKClick();
		}
		else if (source == jButton1) {
			btnCancelClick();
		}
	}
	private void btnOKClick() {
		String name = new String(jTextField3.getText());
		String pw0 = new String(jPasswordField0.getPassword());
		String pw1 = new String(jPasswordField1.getPassword());
		String pw2 = new String(jPasswordField2.getPassword());
		if(name.equals(""))
		{
			JOptionPane.showMessageDialog(jButton0, "用户名不能为空","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if (pw0.equals("") || pw1.equals("") || pw2.equals("")) {
			JOptionPane.showMessageDialog(jButton0, "密码不能为空","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(pw1.length()<6 || pw1.length()>10)
		{
			JOptionPane.showMessageDialog(jButton0, "密码长度范围在6-10之间","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(pw2.length()<6 || pw2.length()>10)
		{
			JOptionPane.showMessageDialog(jButton0, "确认密码长度范围在6-10之间","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		if(!pw2.equals(pw1))
		{
			JOptionPane.showMessageDialog(jButton0, "确认密码与密码必须相同","提示",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		User u = new User(name, pw0);
		boolean b = new IMAlterPwClientConServer().sendAlterPwInfoToServer(u, pw1);
		if(b){
			JOptionPane.showMessageDialog(jButton0, "密码修改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
			btnCancelClick();
		}
		else JOptionPane.showMessageDialog(jButton1, "密码修改未成功！", "失败", JOptionPane.INFORMATION_MESSAGE);
	}
	private void btnCancelClick() {
		this.dispose();
	}
}
